/*
 * author : Dinh Ngoc Tuan Vu (A0092633)
 */
package sg.edu.nus.iss.team4ft.service;

import java.io.Serializable;
import java.util.Date;

import sg.edu.nus.iss.team4ft.util.DateUtil;

public class Customer implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String customerId;
	private String firstName;
	private String lastName;
	private String NRIC;
	private Address billingAddress;
	private Long telephoneNumber;
	private String interest;
	private Boolean status;
	private Date createDate;
	private Date deactivateDate;
	
	public Customer(String firstName, String lastName, String NRIC, Address billingAddress, long contactNumber, String interest){
		generateNewCustomerId();
		setName(firstName, lastName);
		setNRIC(NRIC);
		setBillingAddress(billingAddress);
		if(contactNumber != 0)
			setTelephoneNumber(contactNumber);
		setInterest(interest);
		setCreateDate(DateUtil.getDate());
		setStatus(this.isActive());
	}
	
	protected Customer(Customer customer) throws CloneNotSupportedException{
		setCustomerId(customer.getCustomerId());
		setName(customer.getFirstName(), customer.getLastName());
		setNRIC(customer.getNRIC());
		setBillingAddress(customer.getBillingAddress());
		if(customer.getTelephoneNumber() != null){
			setTelephoneNumber(customer.getTelephoneNumber());
		}
		if (customer.getInterest() != null){
			setInterest(customer.getInterest());
		}
		
		setCreateDate(customer.getCreateDate());
		setDeactivateDate(customer.getDeactivateDate());
		setStatus(customer.isActive());
	}
	
	public void setName(String firstName, String lastName){
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	public String getName(){
		return this.firstName + " " + this.lastName;
	}
	
	public String getFirstName(){
		if(this.firstName != null){
			return this.firstName;
		}else{
			return null;
		}
		
	}
	
	public void setFirstName(String firstName) throws Exception{
		if(firstName == "")
			throw new Exception("First name cannot be null");
		this.firstName = firstName;
	}
	
	public void setLastName(String lastName){
		this.lastName = lastName;
	}
	
	public String getLastName() {
		return this.lastName;
	}
	
	public void setNRIC(String nRIC) {
		NRIC = nRIC;
	}
	
	public String getNRIC() {
		return NRIC;
	}
	
	public boolean NRICEquals(String NRIC) {
		return this.NRIC.equals(NRIC);
	}
	
	public void setBillingAddress(Address billingAddress) {
		this.billingAddress = billingAddress;
	}
	
	public Address getBillingAddress() throws CloneNotSupportedException {
		//return this.billingAddress.clone();
		return this.billingAddress;
	}
	public void setTelephoneNumber(long telephoneNumber) {
		this.telephoneNumber = telephoneNumber;
	}
	
	public Long getTelephoneNumber() {
		return this.telephoneNumber;
	}
	
	public void setInterest(String interest) {
		this.interest = interest;
	}
	
	public String getInterest() {
		return this.interest;
	}
	
	public void setStatus(Boolean status) {
		if(status != null){
			this.status = status;
			if(!status)
				this.deactivateDate = DateUtil.getDate();
			else
				this.deactivateDate = null;
		}else{
			this.status = true;
		}
	}
	
	public Boolean isActive() {
		return this.status;
	}
	
	public boolean isActive(Date forMonth) {
		boolean isActive = false;
		//Check if the subscription is made before the date
		if(DateUtil.dateIsEqual(createDate, forMonth) || DateUtil.dateIsBefore(createDate, forMonth))
			{			
				//check if the date falls below the end date if end date is not null
				//assumed: user may want to unsubscribe a plan for future date. 
				if(getDeactivateDate() == null || DateUtil.dateIsEqual(forMonth, getDeactivateDate()) || DateUtil.dateIsBefore(forMonth, getDeactivateDate()))
				isActive = true;
			}
		
		return isActive;		
	}
	
	@Override
	protected Customer clone() throws CloneNotSupportedException {
		Customer c = new Customer(this);
		return c;
	}
	
	//Overridden from Object Class 
//	public String toString(){
//		return getName() + "\n" + this.NRIC + "\n" + this.billingAddress.toString() + "\n" + this.telephoneNumber + "\n" + this.interest;
//	}
	
	public String getCustomerId() {
		return customerId;
	}
	
	private void setCustomerId(String customerId) {
		this.customerId = customerId;
	}
	
	private String generateNewCustomerId() {
		String id = "CUS" + DateUtil.format(DateUtil.getDate(), "yyMMddHHmmss");
		setCustomerId(id);
		return id;
	}
	
	public boolean equals(Long telephoneNumber){
		if (this.telephoneNumber == telephoneNumber){
			return true;
		}else{
			return false;
		}
	}
	public boolean equals(Customer customer){
		if (this.billingAddress == null){
			if (this.customerId.equals(customer.customerId) && this.firstName.equals(customer.firstName)
					&& this.lastName.equals(customer.lastName) && this.NRIC.equals(customer.NRIC)
					//&& this.billingAddress.equals(customer.billingAddress) 
					&& this.telephoneNumber != null && this.telephoneNumber.equals(customer.telephoneNumber)){
				return true;
			}else{
				return false;
			}
		}else{
			if (this.customerId.equals(customer.customerId) && this.firstName.equals(customer.firstName)
					&& this.lastName.equals(customer.lastName) && this.NRIC.equals(customer.NRIC)
					&& this.billingAddress.equals(customer.billingAddress) 
					&& this.telephoneNumber != null && this.telephoneNumber.equals(customer.telephoneNumber)){
				return true;
			}else{
				return false;
			}
		}
		
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Date getDeactivateDate() {
		return deactivateDate;
	}
	public void setDeactivateDate(Date deactivateDate) {
		this.deactivateDate = deactivateDate;
	}
}
